#include "qaddcomboitemdialog.h"
#include "ui_qaddcomboitemdialog.h"

#include "stylesheeteditor/qresourceview.h"
#include "../qsoftcore.h"
#include "../qfilemanager.h"

#include "../../../libs/core/qnamevalidator.h"
#include "../../../libs/core/qfilemanager.h"
#include "../../../libs/shared/qprojectcore.h"
#include "../../../libs/shared/resource/qresourcemanager.h"

QAddComboItemDialog::QAddComboItemDialog(const QStringList &list,
                                         const QString &text,
                                         const QString &icon,
                                         QWidget *parent):
    QBaseDialog(parent),
    ui(new Ui::QAddComboItemDialog),
    m_text_edit(new QNameEdit(".*")),
    m_icon(icon)
{
    ui->setupUi(this);
    ui->horizontalLayout->addWidget(m_text_edit);
    m_text_edit->setIntermediateList(list);
    m_text_edit->setText(text);

    if(m_icon!="")
    {
        QPixmap pix(":/"+m_icon);
        pix=pix.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation);
        ui->icon_widget->setPixmap(pix);
    }

    m_text_edit->setFocus();
}

QAddComboItemDialog::~QAddComboItemDialog()
{
    if(m_last_file!="")
    {
        QSoftCore::get_core()->get_project_core()->get_resource_manager()->removeResource(m_last_file);
    }
    delete ui;
}

void QAddComboItemDialog::on_okBtn_clicked()
{
    m_text=m_text_edit->value();

    if(m_text=="")
    {
        return;
    }
    m_last_file="";
    close();
}

void QAddComboItemDialog::on_cancelBtn_clicked()
{
    close();
}

QString QAddComboItemDialog::get_text()
{
    return m_text;
}

QString QAddComboItemDialog::get_icon()
{
    return m_icon;
}

void QAddComboItemDialog::on_iconBtn_clicked()
{

    QResourceView dlg(this);

    dlg.exec();

    if(dlg.get_ret()==1)
    {
        tagFileInfo *info=dlg.get_file();
        QString s=QSoftCore::get_core()->get_file_manager()
                ->get_group(info->m_group_uuid)->m_group_name+"/"+info->m_file_name;
        QSoftCore::get_core()->get_project_core()->get_resource_manager()
                ->addResource(s,info->m_file_data);
        QPixmap pix(":/"+s);
        pix=pix.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation);
        ui->icon_widget->setPixmap(pix);
        if(m_last_file!="")
        {
            QSoftCore::get_core()->get_project_core()->get_resource_manager()->removeResource(m_last_file);
        }
        m_last_file=s;
        m_icon=s;
    }
}
